import { App, mall, navigate, toast } from '../../../../../library/api/index'
Page({
  data: {
    navMe: 'on',
    tip: 'blank',
    tipData: {},
    orderId: '',
    returnInfo: ''
  },
  onLoad: function(e) {
    this.setData({
      orderId: e.order_id
    })
    App.jice.onLoad(e)
  },
  onReady: function() {
    App.jice.track('page', {
      pagepath: '/pages/me/order/return/express/index',
      pageTitle: '个人中心-订单-退货物流填写',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '个人中心-订单-退货物流填写'
    })
  },
  onShow: function() {
    this.getReturnStatus()
    App.jice.track('appshow')
  },
  getReturnStatus: function() {
    toast.show()
    mall.getReturnStatus(this.data.orderId, r => {
      console.log(r)
      toast.close()
      if(r.code == 200) {
        this.setData({
          returnInfo: r.data.returninfo
        })
      } else {
        this.showErrorTip(r.data)
      }
    }, r => {
      console.log(r)
      this.showErrorTip('系统错误，请稍候再试！')
    })
  },
  submit: function(e) {
    console.log(e)
    let name = e.detail.value.name
    let code = e.detail.value.code
    let form_id = e.detail.formId
    if(name == '') {
      this.showErrorTip('退货物流信息填写错误，快递公司的名称不能为空')
      return
    }
    if(code == '') {
      this.showErrorTip('退货物流信息填写错误，运单号码不能为空')
      return
    }
    if(!(/^[0-9]+$/.test(code))) {
      this.showErrorTip('退货物流信息填写错误，运单号码格式不正确')
      return
    }
    mall.sendReturnBack({
      order_id: this.data.orderId,
      name: name,
      code: code,
      form_id: form_id
    }, r => {
      console.log(r)
      this.showErrorTip('退货的物流信息已收到，请您耐心等待商家处理')
      setTimeout(() => {
        navigate.back('/pages/pages/me/order/index')
      }, 2000)
    }, r => {
      console.log(r)
      this.showErrorTip('系统错误，请稍后再试')
    })
  },
  showErrorTip: function(message) {
    this.setData({
      tip: 'error_tip',
      tipData: message
    })
  },
  closeTip: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  }
})